var toExam = (function ($) {
	var sourceData;
	var pageType;
	var fromPage; 
	var quesType={'S':'单选题','M':'多选题','B':'填空题','A':'问答题'}
	var hasAskQue = false;
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	// 更新考试成绩
	var refreshExamineUI = function(data,queDict){
		//没有任何操作的返回
		$('.examAnalyBtns')[0].style.display="flex";
		$('.examineInfo')[0].classList.add('grayColor')
		var toExamBtn = $('#toExamBtn')[0]
		
		var score = 0
		var count = 0
		var askHtml = '<div class="scoreTitle">答题卡</div><div class="answerSheet">'
		var ReleasePass = parseInt(data['ReleasePass'])
		for (var i = 0; i < queDict.length;i++) {
			var oneDict = queDict[i]
			var type = oneDict['SubMode']
			var GetScore = parseInt(oneDict['GetScore'])
			var ExamScore = oneDict['ExamScore']
			console.log(ExamScore + ' '+oneDict['Result'])
			var ansItemClass = ''
			if (ExamScore != oneDict['GetScore']) {
				count++
				if (type == 'A') {
					ansItemClass = 'waiting'
				}else if (oneDict['Result'] != '') {
					if (type == 'B') {
						var blankAns = oneDict['Result'].split('」')
						for (var j = 0;j < blankAns.length;j++) {
							if (blankAns[j]!="") {
								ansItemClass = 'error'
								break
							}
						}
					}else{
						ansItemClass = 'error'
					}
				}
			}else{
				ansItemClass = 'correct'
			}
			askHtml +='<div class="ansItem '+ansItemClass+'">'+(i+1)+'</div>'
			if (!isNaN(GetScore)) {
				score += GetScore
			}
		}
		var infoDom = $('.scoreInfo')[0]
		if (hasAskQue) { //有问答题时
			infoDom.innerHTML = askHtml +'</div><div class="scoreCount">问答题等待人工阅卷，共解答'+queDict.length+'题，用时 '+data['ExamTime']+'</div>';;
			toExamBtn.style.display='none'
			return
		}
		var html = '<div class="scoreTitle">考试成绩</div>'
		if (score >= ReleasePass) {//通过
			html += '<div class="scoreResult">通过</div>'
			toExamBtn.style.display = 'none';
			html += '<div class="scoreText">'+score+'<span >分</span></div>'+
				'<div class="scoreCount">答对'+(queDict.length - count)+'道题，答错'+count+'道题，用时 '+data['ExamTime']+'</div>';
			infoDom.innerHTML = html;
			return
		}
		
		//是否可重考N:否，Y:是
		if (sourceData['ReleaseResit'] == 'N') { //不可重考
			toExamBtn.style.display = 'none';
			html += '<div class="scoreResult redColor">未通过</div>'
			toExamBtn.style.display = 'none';
			html += '<div class="scoreText">'+score+'<span >分</span></div>'+
				'<div class="scoreCount">答对'+(queDict.length - count)+'道题，答错'+count+'道题，用时 '+data['ExamTime']+'</div>';
			return
		}
		var times = toExamBtn.getAttribute('data-time')
		if (times != "N") {
			times = parseInt(times) -1;
		}
		toExamBtn.setAttribute('data-time',times)
		if (times == 'N') {//不限次数
			html += '<div class="scoreResult redColor">未通过</div>'
			toExamBtn.innerHTML = '重考';
		}else if (times > 0) {//未通过可以复考
			html += '<div class="scoreResult redColor">未通过</div>'
			toExamBtn.innerHTML = '重考（'+times+'）';
		}else{//复考未通过
			html += '<div class="scoreResult redColor">未通过</div>'
			toExamBtn.innerHTML = '重考（'+times+'）';
			toExamBtn.style.background = 'gray'
			toExamBtn.classList.add('noTimes')
		}
		html += '<div class="scoreText">'+score+'<span >分</span></div>'+
			'<div class="scoreCount">答对'+(queDict.length - count)+'道题，答错'+count+'道题，用时 '+data['ExamTime']+'</div>';
		infoDom.innerHTML = html;
	}
	
	
	function examAndAnalyBtnTapped(){
		var id = this.id;
		if (this.getAttribute('class').indexOf('noTimes') !=-1) {
			mui.toast('考试次数用完')
			return;
		}
		var isAnaly = false
		if (id != 'toExamBtn') {
			isAnaly = true
		}
		
		var webview = plus.webview.create('examRealPaper.html', 'examRealPaper', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{fromPage:'toExamine',pageType:'','isAnaly':isAnaly,sourceData:sourceData});
		webview.show('fade-in');
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		$('#toExamBtn')[0].addEventListener('tap',examAndAnalyBtnTapped)
		mui(".examAnalyBtn").each(function() {
			this.addEventListener('tap',examAndAnalyBtnTapped)
		})
		var self = plus.webview.currentWebview()
		sourceData = self.sourceData;
		pageType = self.pageType;
		fromPage = self.fromPage;
		// 允许考试次数
		$('#toExamBtn')[0].setAttribute('data-time',sourceData['ReleaseResitNum'])
		var subSum = 0
		var rangDict = {'S':0,'M':0,'B':0,'A':0}
		var QuestRange = sourceData['QuestRange'].split('「') //"S」」5」5」25」13「M」」2」5」10」13「B」」2」10」20」13「A」」2」20」40」13",
		for (var i=0; i < QuestRange.length;i++) { 
			var oneRange=QuestRange[i].split('」')
			if (oneRange != undefined) {
				var onenum = parseInt(oneRange[2])
				subSum += onenum
				rangDict[oneRange[0]] += onenum
			}
		}
		sourceData['SubSum'] = subSum
		if (rangDict['A'] != 0) { //有问答题
			hasAskQue = true
		}
		var rangeHtml = (rangDict['S']==0?'':('单选'+rangDict['S']+'题，'))
		rangeHtml += (rangDict['M']==0?'':('多选'+rangDict['M']+'题，'))
		rangeHtml += (rangDict['B']==0?'':('填空'+rangDict['B']+'题，'))
		rangeHtml += (rangDict['A']==0?'':('问答'+rangDict['A']+'题，'))
		if (rangeHtml != '') {
			rangeHtml = rangeHtml.substring(0,rangeHtml.length-1)
		}
		var TimeList = sourceData['TimeList'].split('」')
		html = '考核类型： 理论考核 </br>'+
			'考试标准： '+sourceData['Duration']+'分钟，'+subSum+'题</br>'+
			'考试范围： '+ rangeHtml +'</br>'+
			'合格标准： '+sourceData['ReleasePass']+'分及格（满分'+sourceData['Score']+'分） </br>'+
			'考核时间：'+TimeList[0].replace('-','.')+' - '+TimeList[1].replace('-','.')
		$('#examineInfo')[0].innerHTML = html
		
	}
	var exportObj = {}
	exportObj.refreshExamineUI = refreshExamineUI;
	return exportObj
})(mui)
function refreshExamineUI(data1,queDict){
	toExam.refreshExamineUI(data1,queDict);
}